<template>
  <div class="user">
    <div class="common">
      <div class="common-input">
        <el-input v-model="commonValue" placeholder="用户昵称/手机号码" clearable></el-input>
      </div>



      <div class="common-input">
        <el-date-picker v-model="commonDate" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div class="common-button" @click="replacementHit">重置</div>
    </div>

    <div class="table" v-loading="loading">
      <el-table :data="tableData" border :header-cell-style="{
        color: '#333333',
        backgroundColor: '#f3f3f3',
      }">
        <el-table-column prop="userId" label="用户id" width="100" align="center">
        </el-table-column>
        <el-table-column prop="" label="用户头像" align="center">
          <template slot-scope="scope">
            <img class="imgs" :src="scope.row.avatar" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="用户昵称"> </el-table-column>
        <el-table-column prop="phone" label="手机号码"> </el-table-column>
        <el-table-column prop="balance" label="用户余额"> </el-table-column>
        <el-table-column prop="integral" label="用户积分"> </el-table-column>
        <el-table-column prop="createTime" label="注册时间"> </el-table-column>
        <!-- <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 0" type="success">普通用户</el-tag>
            <el-tag v-else type="info">租户</el-tag>
          </template>
        </el-table-column> -->

        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="detailsHit(scope.row)">
                查看详情
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          layout="prev, pager, next" :total="tableDataTotal" :current-page="page">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  userList
} from '@/api/user'

export default {
  data() {
    return {
      commonValue: "",
      // commonUpdateOptions: [
      //   {
      //     value: "0",
      //     label: "全部",
      //   },
      //   {
      //     value: "1",
      //     label: "租户",
      //   },
      //   {
      //     value: "2",
      //     label: "普通用户",
      //   },
      // ],
      // commonUpdateValue: "",
      commonDate: [],
      commonStart: "",
      commonEnd: "",
      //-------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      loading: false,
      commonObj: {},
    };
  },
  created() {
    this.userListMet()
  },
  methods: {
    userListMet() {
      let params = {
        keyWord: this.commonValue,
        startTime: this.commonStart,
        endTime: this.commonEnd,
        pageSize: this.limit,
        pageNo: this.page,
      }

      userList(params).then(res => {
        if (res.errorCode == null) {
          res.resultData.userList.forEach((item, index) => {
            item.balance = Number(item.balance / 100).toFixed(2);
          });

          this.tableData = res.resultData.userList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      })
    },
    timeData(date) {
      return date.toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
      });
    },
    searchHit() {
      if (this.commonDate.length > 0) {
        this.commonStart = this.timeData(new Date(this.commonDate[0]));
        this.commonEnd = this.timeData(new Date(this.commonDate[1]));
      } else {
        this.commonStart = "";
        this.commonEnd = "";
      }

      this.page = 1;
      this.loading = true;
      this.userListMet();
    },
    replacementHit() {
      this.commonDate = [];
      this.commonStart = "";
      this.commonEnd = "";
      this.commonValue = "";

      this.page = 1;
      this.loading = true;
      this.userListMet();
    },
    //-----------------------------
    detailsHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error('暂无权限，请联系管理员开通')
        return;
      }

      this.$router.push({
        path: "/user/list-details?id=" + item.userId,
      });
    },
    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.userListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.userListMet();
    },
  },
};
</script>

<style lang="less" scoped>
.user {
  .common {
    margin-bottom: 10px;
    display: flex;
    align-items: center;

    .common-input {
      margin-right: 10px;

      /deep/ .el-input {
        width: 250px;
      }
    }

    .common-input:last-child {
      margin-right: 0;
    }

    .common-button {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      margin-left: 20px;
      cursor: pointer;
    }

    .common-button:last-child {
      background: #999999;
    }
  }

  .table {
    cursor: pointer;

    .operation {
      display: flex;
      align-items: center;
      color: #2a82e4;

      .list {
        margin-right: 20px;
        cursor: pointer;
      }
    }

    .imgs {
      width: 50px;
      height: 50px;
    }
  }
}
</style>